package com.astroway.livetest;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.astroway.livetest.model.MySelfInfo;
import com.astroway.livetest.presenters.InitBusinessHelper;
import com.astroway.livetest.presenters.LoginHelper;
import com.astroway.livetest.presenters.viewinface.LoginView;
import com.astroway.livetest.util.SharedUtil;
import com.tencent.ilivesdk.ILiveCallBack;
import com.tencent.ilivesdk.core.ILiveLoginManager;

import java.util.HashMap;
import java.util.Set;

public class MainActivity extends BaseActivity implements View.OnClickListener, LoginView {
    private static final String TAG = "MainActivity";
    private static final int WAHT1 = 10001;
    private EditText nameEt, psdEt, hostIdEt, roomNumEt;
    private Button loginBtn, registerBtn;
    private LoginHelper mLoginHeloper;
    SharedUtil sharedUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitBusinessHelper.initApp(getApplicationContext());
        mLoginHeloper = new LoginHelper(this, this);
        checkPermission();
        //获取个人数据本地缓存
        MySelfInfo.getInstance().getCache(getApplicationContext());
        initView();
        setShareData();
        //        if (needLogin() == true) {//本地没有账户需要登录
        //        } else {
        //            //有账户登录直接IM登录
        //            SxbLog.i(TAG, "LoginActivity onCreate");
        //            mLoginHeloper.iLiveLogin(MySelfInfo.getInstance().getId(), MySelfInfo.getInstance().getUserSig());
        //        }
    }

    private void setShareData() {
        sharedUtil = new SharedUtil(this);
//        nameEt.setText(sharedUtil.getData("account", "17685757560").toString());
//        roomNumEt.setText(sharedUtil.getData("roomNum", "").toString());
    }

    private void initView() {
        nameEt = (EditText) findViewById(R.id.activity_main_name_et);
        psdEt = (EditText) findViewById(R.id.activity_main_psd_et);
        loginBtn = (Button) findViewById(R.id.activity_main_login_btn);
        registerBtn = (Button) findViewById(R.id.activity_main_register_btn);
        hostIdEt = (EditText) findViewById(R.id.activity_main_hostid_et);
        roomNumEt = (EditText) findViewById(R.id.activity_main_roomid_et);
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_main_login_btn:
                loginIn();
                break;
            case R.id.activity_main_register_btn:
                toRegister();
                break;
        }
    }

    private void loginIn() {
        if (nameEt.getText().equals("")) {
            Toast.makeText(MainActivity.this, "name can not be empty!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (psdEt.getText().equals("")) {
            Toast.makeText(MainActivity.this, "password can not be empty!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (hostIdEt.getText().equals("")) {
            return;
        }
        if (roomNumEt.getText().equals("")) {
            return;
        }
        sharedUtil.saveData("account", nameEt.getText().toString());
        sharedUtil.saveData("roomNum", roomNumEt.getText().toString());
        mLoginHeloper.standardLogin(nameEt.getText().toString(), psdEt.getText().toString());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //        mLoginHeloper.standardLogout(MySelfInfo.getInstance().getId());
    }

    @Override
    public void loginSucc() {
        mHandler.sendEmptyMessage(WAHT1);
    }

    @Override
    public void loginFail(String module, int errCode, String errMsg) {
        Log.i(TAG, "loginFail: " + module + "===" + errMsg);
        Toast.makeText(MainActivity.this, "登录失败:", Toast.LENGTH_SHORT).show();
    }

    /**
     * 判断是否需要登录
     *
     * @return true 代表需要重新登录
     */
    public boolean needLogin() {
        if (MySelfInfo.getInstance().getId() != null) {
            return false;//有账号不需要登录
        } else {
            return true;//需要登录
        }

    }

    private void toRegister() {
        String userId = nameEt.getText().toString();
        String userPW = psdEt.getText().toString();


        if (userId.length() < 4 || userId.length() > 24) {
            Log.i(TAG, "onClick " + userId.length());
            Toast.makeText(MainActivity.this, "用户名不符合格式", Toast.LENGTH_SHORT).show();
            return;
        }


        if (userId.length() == 0 || userPW.length() == 0) {
            Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        if (userPW.length() < 6) {
            Toast.makeText(MainActivity.this, "密码的长度不能小于6个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        //注册一个账号
        ILiveLoginManager.getInstance().tlsRegister(userId, psdEt.getText().toString(), new ILiveCallBack() {
            @Override
            public void onSuccess(Object data) {
                Log.i(TAG, "onSuccess: ");
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                Log.i(TAG, "onError: ");
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        //        mLoginHeloper.standardLogout(MySelfInfo.getInstance().getId());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoginHeloper.onDestory();
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case WAHT1:
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, LiveActivity.class);
                    intent.putExtra("HostId", hostIdEt.getText().toString().trim());
                    intent.putExtra("RoomNum", Integer.valueOf(roomNumEt.getText().toString().trim()));
                    intent.putExtra("Phone", nameEt.getText().toString());
                    startActivity(intent);
                    //                    finish();
                    break;
            }
        }
    };
}
